package et.item;

import et.common.ElectricThaum;
import ic2.api.item.ElectricItem;
import ic2.api.item.IElectricItem;
import ic2.core.IC2;
import ic2.core.item.ItemBattery;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

/**
 * Created by winston_wang on 2017/6/8.
 */
public class ETItemBattery extends Item implements IElectricItem{
    public int maxCharge;
    public int transferLimit;
    public int tier;
    public ETItemBattery(){
        this(10000, 32, 1);
    }
    public ETItemBattery(int maxCharge, int transferLimit, int tier){
        this.maxCharge =maxCharge;
        this.transferLimit =transferLimit;
        this.tier =tier;
        //setUnlocalizedName("thaumBattery");
        //setTextureName("et:itemBatThaum");
        setCreativeTab(ElectricThaum.tabET);
        setNoRepair();
        setMaxDamage(27);
    }
    @Override
    public boolean canProvideEnergy(ItemStack itemStack) {
        return true;
    }
    @Override
    public Item getChargedItem(ItemStack itemStack) {
        return this;
    }
    @Override
    public Item getEmptyItem(ItemStack itemStack) {
        return this;
    }
    @Override
    public double getMaxCharge(ItemStack itemStack) {
        return maxCharge;
    }
    @Override
    public int getTier(ItemStack itemStack) {
        return tier;
    }
    @Override
    public double getTransferLimit(ItemStack itemStack) {
        return transferLimit;
    }

    public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer entityplayer) {
        if(IC2.platform.isSimulating() && itemStack.getItem() == this) {
            boolean transferred = false;
            for(int i = 0; i < 27; ++i) {
                ItemStack stack = entityplayer.inventory.getStackInSlot(i);
                if(stack != null && !(stack.getItem() instanceof ItemBattery)) {
                    double transfer = ElectricItem.manager.discharge(itemStack, 2.0D * this.transferLimit, 2147483647, true, true, true);
                    if(transfer > 0.0D) {
                        transfer = ElectricItem.manager.charge(stack, transfer, this.tier, true, false);
                        if(transfer > 0.0D) {
                            ElectricItem.manager.discharge(itemStack, transfer, 2147483647, true, true, false);
                            transferred = true;
                        }
                    }
                }
            }

            if(transferred && !IC2.platform.isRendering()) {
                entityplayer.openContainer.detectAndSendChanges();
            }
        }

        return itemStack;
    }
}
